package com.qiusuo0226.self.controller;

import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.self.pojo.dto.PasswordManageAddDTO;
import com.qiusuo0226.self.pojo.dto.PasswordManageEditDTO;
import com.qiusuo0226.self.service.PasswordManageService;
import com.qiusuo0226.self.pojo.query.PasswordManageSearchBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 密码管理 前端控制层
 * @author qiusuo
 * @date 2023/7/15 21:37
 */
@RestController
@RequestMapping("/manager/passwordManage")
@Validated
@Slf4j
public class PasswordManageController {

    @Autowired
    private PasswordManageService passwordManageService;

    /**
     * 获取个人密码管理列表
     * @return
     */
    @PostMapping("/list")
    public JsonResultModel list(@RequestBody @Valid PasswordManageSearchBean bean) {

        return JsonResultModel.success(passwordManageService.list(bean));
    }

    /**
     * 个人密码管理：新增
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public JsonResultModel add(HttpServletRequest request, @RequestBody @Valid PasswordManageAddDTO dto) {
        try {
            // 把入参中需要解密的字段新转为明文
            dto.decrypt();
        } catch (Exception e) {
            log.error("个人密码管理新增入参解密异常：", e);
            return JsonResultModel.paramsError("入参解密异常");
        }
        // 获取请求的Token
        dto.setToken(request.getHeader("Authorization"));
        return passwordManageService.add(dto);
    }

    /**
     * 个人密码管理：编辑
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    public JsonResultModel edit(HttpServletRequest request, @RequestBody @Valid PasswordManageEditDTO dto) {
        try {
            // 把入参中需要解密的字段新转为明文
            dto.decrypt();
        } catch (Exception e) {
            log.error("个人密码管理新增入参解密异常：", e);
            return JsonResultModel.paramsError("入参解密异常");
        }
        // 获取请求的Token
        dto.setToken(request.getHeader("Authorization"));
        return passwordManageService.edit(dto);
    }

    /**
     * 个人密码管理：删除
     * @param iids 待删除数据的主键唯一标识，多个主键用英文逗号分割
     * @param creater 创建者唯一标识
     * @return
     */
    @PostMapping("/remove")
    public JsonResultModel remove(@NotBlank(message = "待删除主键不能为空") String iids,
                                  @NotNull(message = "创建者唯一标识不能为空") Long creater) {
        return passwordManageService.remove(iids, creater);
    }
}
